package v1

import (
	"gitee.com/CrazyMage/lite-note/internal/controller/http/v1/request"
	"gitee.com/CrazyMage/lite-note/internal/controller/http/v1/response"
	"gitee.com/CrazyMage/lite-note/pkg/utils"
	"github.com/gin-gonic/gin"
)

func (r *V1) CreateNoteHandler(ctx *gin.Context) {

	//1.请求参数
	var req request.CreateNoteReq

	//2.绑定参数
	if err := ctx.ShouldBind(&req); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//3.参数转换
	serviceReq := &request.ServiceCreateNoteReq{}
	if err := utils.Convert(&req, &serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//5.执行业务
	if resp, err := r.n.CreateNote(ctx.Request.Context(), serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.ERROR, err.Error())
		return
	} else {
		//6.返回结果
		response.OkWithData(ctx, response.SUCCESS, response.GetMsg(response.SUCCESS), resp)
	}

}

func (r *V1) ListNoteHandler(ctx *gin.Context) {
	//1.请求参数
	var req request.ListNoteReq

	//2.绑定参数
	if err := ctx.ShouldBindJSON(&req); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, err.Error())
		return
	}

	//3.参数转换
	serviceReq := &request.ServiceListNodeReq{}
	if err := utils.Convert(&req, &serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//5.执行业务
	if resp, err := r.n.ListNode(ctx.Request.Context(), serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.ERROR, err.Error())
		return
	} else {
		//6.返回结果
		response.OkWithData(ctx, response.SUCCESS, response.GetMsg(response.SUCCESS), resp)
	}
}

func (r *V1) ShowNoteHandler(ctx *gin.Context) {
	//1.请求参数
	var req request.ShowNoteReq

	//2.绑定参数
	if err := ctx.ShouldBindUri(&req); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//3.参数转换
	serviceReq := &request.ServiceShowNoteReq{}
	if err := utils.Convert(&req, &serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//5.执行业务
	if resp, err := r.n.ShowNote(ctx.Request.Context(), serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.ERROR, err.Error())
		return
	} else {
		//6.返回结果
		response.OkWithData(ctx, response.SUCCESS, response.GetMsg(response.SUCCESS), resp)
	}
}

func (r *V1) DeleteNoteHandler(ctx *gin.Context) {
	//1.请求参数
	var req request.DeleteNoteReq

	//2.绑定参数
	if err := ctx.ShouldBindUri(&req); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//3.参数转换
	serviceReq := &request.ServiceDeleteNoteReq{}
	if err := utils.Convert(&req, &serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//5.执行业务
	if resp, err := r.n.DeleteNote(ctx.Request.Context(), serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.ERROR, err.Error())
		return
	} else {
		//6.返回结果
		response.OkWithData(ctx, response.SUCCESS, response.GetMsg(response.SUCCESS), resp)
	}
}

func (r *V1) UpdateNoteHandler(ctx *gin.Context) {
	//1.请求参数
	var req request.UpdateNoteReq

	//2.绑定参数
	if err := ctx.ShouldBindJSON(&req); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//3.参数转换
	serviceReq := &request.ServiceUpdateNoteReq{}
	if err := utils.Convert(&req, &serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}
	r.l.Info("serviceReq", serviceReq)

	//5.执行业务
	if resp, err := r.n.UpdateNote(ctx.Request.Context(), serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.ERROR, err.Error())
		return
	} else {
		//6.返回结果
		response.OkWithData(ctx, response.SUCCESS, response.GetMsg(response.SUCCESS), resp)
	}
}

func (r *V1) SearchNoteHandler(ctx *gin.Context) {
	//1.请求参数
	var req request.SearchNoteReq

	//2.绑定参数
	if err := ctx.ShouldBind(&req); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//3.参数转换
	serviceReq := &request.ServiceSearchNoteReq{}
	if err := utils.Convert(&req, &serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.InvalidParams, response.GetMsg(response.InvalidParams))
		return
	}

	//5.执行业务
	if resp, err := r.n.SearchNote(ctx.Request.Context(), serviceReq); err != nil {
		r.l.Error(err.Error())
		response.Error(ctx, response.ERROR, err.Error())
		return
	} else {
		//6.返回结果
		response.OkWithData(ctx, response.SUCCESS, response.GetMsg(response.SUCCESS), resp)
	}
}
